<script lang="ts" setup>
import type {FormInstance, FormRules} from 'element-plus';
import {useOptionStore} from '@/store/modules/selectOption'
import {storeToRefs} from "pinia";
import {DICT_TYPE, getDictOptions} from "@/utils/dict";
import * as projectApi from "@/api/business/project";


defineOptions({name: "ProjectForm"})
const {t} = useI18n() // 国际化
const message = useMessage()
const emit = defineEmits(['success'])

const optionStore = useOptionStore()
const {userOption} = storeToRefs(optionStore)
const directorOptions = computed(() => userOption.value.filter(item => !form.value.members.includes(item.value)))
const memberOptions = computed(() => userOption.value.filter(item => form.value.director !== item.value))


const disabled = computed(() => {
  return type.value === 'detail'
})
const type = ref('detail')
const title = ref('详情')
const dialogVisible = ref(false)
const formLoading = ref(false)
const companyOptions = ref([
  {
    value: 'Option1',
    label: 'Option1',
  }
])
const peopleOptions = ref([
  {
    value: 'Option1',
    label: 'Option1',
  }
])
const formRef = useTemplateRef<FormInstance>('formRef')
const form = ref({
  id: void 0,
  projectName: void 0,//项目名称
  partyA: void 0,//甲方单位
  liaison: void 0,//对接人
  director: void 0,//负责人
  members: [],//成员
  producer: void 0,//制作人
  content: void 0,
  startDate: void 0,
  endDate: void 0,
  submissionDate: void 0,//预估交稿时间
  projectStatus: void 0,//项目进度
  projectLevel: void 0,//项目等级
  taskStatus: void 0,//任务进度
  remake: void 0,//任务进度
})
const rules = ref<FormRules>({
  projectName: [
    {required: true, message: '请输入项目名称', trigger: 'blur'},
  ],
  partyA: [
    {required: true, message: '请选择甲方单位', trigger: 'change'},
  ],
  liaison: [
    {required: true, message: '请选择对接人', trigger: 'change'},
  ],
  director: [
    {required: true, message: '请选择负责人', trigger: 'change'},
  ],
  members: [
    {required: true, message: '请选择成员', trigger: 'change'},
  ],
  producer: [
    {required: true, message: '请选择制作人', trigger: 'change'},
  ],
  content: [
    {required: true, message: '请输入设计内容', trigger: 'blur'},
  ],
  startDate: [
    {required: true, message: '请选择开始时间', trigger: 'blur'},
  ],
  endDate: [
    {required: true, message: '请选择结束时间', trigger: 'blur'},
  ],
  submissionDate: [
    {required: true, message: '请选择交稿时间', trigger: 'change'},
  ],
  projectStatus: [
    {required: true, message: '请选择项目进度', trigger: 'change'},
  ],
  projectLevel: [
    {required: true, message: '请选择项目等级', trigger: 'change'},
  ],
  remake: [
    {required: true, message: '请输入备注', trigger: 'blur'},
  ],
})

function handleClose() {
  dialogVisible.value = false
  formRef.value?.resetFields()
  form.value = {
    id: void 0,
    projectName: void 0,//项目名称
    partyA: void 0,//甲方单位
    liaison: void 0,//对接人
    director: void 0,//负责人
    members: [],//成员
    producer: void 0,//制作人
    content: void 0,
    startDate: void 0,
    endDate: void 0,
    submissionDate: void 0,//预估交稿时间
    projectStatus: void 0,//项目进度
    projectLevel: void 0,//项目等级
    taskStatus: void 0,//任务进度
    remake: void 0,//任务进度
  }
}

async function open(state: 'add' | 'edit' | 'detail' = "detail", id?: string) {
  type.value = state
  title.value = t('action.' + state)
  dialogVisible.value = true
  if (id) {
    formLoading.value = true
    try {
      form.value = await projectApi.getProject(id)
    } finally {
      formLoading.value = false
    }
  }
}

async function handleSubmit() {
  try {
    await formRef.value?.validate()
    if (type.value === 'add') {
      await projectApi.createProject(form.value)
      message.success(t('common.createSuccess'))
    } else if (type.value === 'edit') {
      await projectApi.updateProject(form.value)
      message.success(t('common.updateSuccess'))
    }
    emit('success')
    handleClose()
  } catch (e) {

  }
}

defineExpose({
  open
})
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    :title
    top="5vh"
    width="50%"
    @close="handleClose"
  >
    <el-form ref="formRef" v-loading="formLoading" :inline="true" :model="form"
             :rules class="-mb-15px" label-width="auto">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item class="w-100%" label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled clearable placeholder="请输入项目名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="项目等级" prop="projectLevel">
            <el-select v-model="form.projectLevel" :disabled clearable
                       placeholder="请选择项目等级">
              <el-option v-for="item in getDictOptions(DICT_TYPE.BUSINESS_PROJECT_LEVEL)"
                         :key="item.value" :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="单位名称" prop="partyA">
            <el-select v-model="form.partyA" :disabled clearable
                       placeholder="请选择单位名称">
              <el-option
                v-for="item in companyOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="甲方对接人" prop="liaison">
            <el-select v-model="form.liaison" :disabled clearable
                       placeholder="请选择甲方对接人">
              <el-option
                v-for="item in peopleOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="负责人" prop="director">
            <el-select v-model="form.director" :disabled clearable
                       placeholder="请选择负责人">
              <el-option
                v-for="item in directorOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="成员" prop="members">
            <el-select v-model="form.members" :disabled filterable multiple
                       placeholder="请选择成员">
              <el-option v-for="item in memberOptions" :key="item.value" :label="item.label"
                         :value="item.value"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="制作人" prop="producer">
            <el-select v-model="form.producer" :disabled clearable
                       placeholder="请选择制作人">
              <el-option
                v-for="item in userOption"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="设计内容" prop="content">
            <el-input v-model="form.content" :disabled clearable placeholder="请输入设计内容"
                      type="textarea"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="开始时间" prop="startDate">
            <el-date-picker v-model="form.startDate" :disabled class="!w-100%"
                            placeholder="开始日期"
                            type="date"
                            value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="结束时间" prop="endDate">
            <el-date-picker v-model="form.endDate" :disabled class="!w-100%" placeholder="结束日期"
                            type="date"
                            value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="交稿时间" prop="submissionDate">
            <el-date-picker v-model="form.submissionDate" :disabled class="!w-100%"
                            placeholder="交稿日期"
                            type="date"
                            value-format="YYYY-MM-DD"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="w-100%" label="项目进度" prop="projectStatus">
            <el-select v-model="form.projectStatus" :disabled clearable
                       placeholder="请选择制作人">
              <el-option
                v-for="item in getDictOptions(DICT_TYPE.BUSINESS_PROJECT_STATUS)"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <!--        <el-col :span="12">-->
        <!--          <el-form-item class="w-100%" label="任务进度" prop="taskStatus">-->
        <!--            <el-select v-model="form.taskStatus" :disabled clearable-->
        <!--                       placeholder="请选择任务进度">-->
        <!--              <el-option-->
        <!--                v-for="item in taskStatusOptions"-->
        <!--                :key="item.value"-->
        <!--                :label="item.label"-->
        <!--                :value="item.value">-->
        <!--              </el-option>-->
        <!--            </el-select>-->
        <!--          </el-form-item>-->
        <!--        </el-col>-->
        <el-col :span="12">
          <el-form-item class="w-100%" label="备注" prop="remake">
            <el-input v-model="form.remake" :disabled clearable placeholder="请输入备注"
                      type="textarea"/>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button v-show="type!=='detail'" type="primary" @click="handleSubmit">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>

</style>
